import { getDataAssetList } from '@/api/test/index';
import { daDataItem, dapostData } from '@/pages/CodeTables/specification';
import { CodeTableStatus, stopwatchColor } from '@/enums/statusEnum';

const table = reactive<{
  data: [];
  total: number;
  daChName: string;
  daEnName: string;
  daStatus: undefined | string;
  directoryCode: string;
  num: number;
  size: number;
}>({
  data: [],
  total: 10,
  daChName: '',
  daStatus: '',
  daEnName: '',
  directoryCode: '',
  num: 1,
  size: 10,
});

const loading = ref<boolean>(true);
const getData = async () => {
  loading.value = true;
  const postList: dapostData = {
    pageNum: table.num,
    pageSize: table.size,
    daStatus: table.daStatus,
    daChName: table.daChName,
    daEnName: table.daEnName,
    directoryCode: table.directoryCode,
  };
  console.log('请求发送');
  try {
    const res = await getDataAssetList(postList);
    res.records.forEach((item: daDataItem) => {
      item.updateTime = new Date(item.updateTime).toLocaleString();
      // 向后端请求数据对象中添加了statusColor用于控制状态颜色
      item.statusColor = stopwatchColor[+item.daStatus];
      item.daStatus = CodeTableStatus[+item.daStatus];
    });
    table.data = res.records;
    table.total = res.total;
    console.log(res.total, '---', table.total);
    loading.value = false;
  } catch (error) {
    loading.value = false;
  }
};

export function getListHooks() {
  return {
    getData,
    table,
    loading,
  };
}
